package com.yzc.android.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;

import butterknife.Bind;
import com.marshalchen.ultimaterecyclerview.ItemTouchListenerAdapter;
import com.marshalchen.ultimaterecyclerview.ItemTouchListenerAdapter.RecyclerViewOnItemClickListener;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView.OnLoadMoreListener;
import com.marshalchen.ultimaterecyclerview.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.yzc.android.R;
import com.yzc.android.network.response.SectionDataResponse;
import com.yzc.android.ui.adapter.RecyclerViewSectionAdapter;
import com.yzc.android.ui.base.activity.KDBaseActivity;
import com.yzc.android.utils.LogUtils;

/**
 * Created by yzc on 16/4/18.
 */
public class RecyclerViewActivity extends KDBaseActivity implements RecyclerViewOnItemClickListener {
    @Bind(R.id.recycler_view)
    public UltimateRecyclerView mRecyclerView;
    protected LinearLayoutManager linearLayoutManager;
    //private RecyclerViewDemoAdapter mAdapter;
    //private RecyclerViewAdapter mViewAdapter;
    private RecyclerViewSectionAdapter mViewAdapter;
    private ItemTouchHelper mItemTouchHelper;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupView(final Bundle savedInstanceState) {
        //mNavigationBar.setTitle("Android Rapid");
        mRecyclerView.setHasFixedSize(true);
        linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mRecyclerView.setRecylerViewBackgroundColor(Color.parseColor("#ffffff"));

        mRecyclerView.setLoadMoreView(R.layout.layout_loadmore_bottom_progressbar);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LogUtils.d("loadMore");
                        mRecyclerView.setRefreshing(false);
                        mRecyclerView.disableLoadmore();
                    }
                },2000);
            }
        });

        //
        //为每个item添加头部的布局   这里运用到的其实就是RecyclerView.ItemDecoration
        //没错  可能一般都是用这个来实现item之间的分隔线的  但是线也是一个view，但这个view够大的时候，就是一个头部了
        StickyRecyclerHeadersDecoration stickyRecyclerHeadersDecoration = new StickyRecyclerHeadersDecoration(mViewAdapter);
        mRecyclerView.addItemDecoration(stickyRecyclerHeadersDecoration);
        //
        //ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter){
        //    //这个方法还有别的方法可以重载  可以控制如滑动删除等功能
        //
        //
        //    @Override
        //    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //        final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//控制拖动的方向   这里设置了智能上下拖动交换位置
        //        final int swipeFlags = ItemTouchHelper.LEFT ;//控制滑动删除的方向  这里设置了只能左滑删除
        //        //                final int swipeFlags = ItemTouchHelper.LEFT |ItemTouchHelper.RIGHT;//左右滑删除
        //
        //        return makeMovementFlags(dragFlags, swipeFlags);
        //    }
        //
        //    @Override
        //    public boolean isItemViewSwipeEnabled() {
        //        return super.isItemViewSwipeEnabled();//这里控制开启或关闭item是否可以滑动删除的功能
        //    }
        //
        //    @Override
        //    public boolean isLongPressDragEnabled() {
        //        return super.isLongPressDragEnabled();//控制长按拖动功能
        //    }
        //};
        //
        //final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        //itemTouchHelper.attachToRecyclerView(mRecyclerView.mRecyclerView);
        //
        //mRecyclerView.setParallaxHeader(R.layout.layout_header_recycler_view);
        mRecyclerView.setNormalHeader(LayoutInflater.from(this).inflate(R.layout.layout_header_recycler_view, null));
        //mRecyclerView.setOnParallaxScroll(new OnParallaxScroll() {
        //    @Override
        //    public void onParallaxScroll(final float percentage, final float offset, final View parallax) {
        //        //Logger.d("onParallaxScroll:percentage=%s, offset=%s, parallax=%s", percentage, offset, parallax);
        //    }
        //});

        //ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mViewAdapter);
        //mItemTouchHelper = new ItemTouchHelper(callback);
        //mItemTouchHelper.attachToRecyclerView(mRecyclerView.mRecyclerView);

        // 处理侧滑删除 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0630/3123.html
        //ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mViewAdapter);
        //mItemTouchHelper = new ItemTouchHelper(callback);
        //mItemTouchHelper.attachToRecyclerView(mRecyclerView.mRecyclerView);

        mRecyclerView.addOnItemTouchListener(new ItemTouchListenerAdapter(mRecyclerView.mRecyclerView, this));
    }

    @Override
    public int getLayout() {
        return R.layout.layout_activity_recycler_view;
    }

    @Override
    public boolean enablePullRefresh() {
        return true;
    }

    @Override
    public void pullToRefreshHandler() {
        mRecyclerView.setRefreshing(false);
        linearLayoutManager.scrollToPosition(0);
    }

    @Override
    public void onItemClick(final RecyclerView parent, final View clickedView, int position) {
        position = mViewAdapter.hasHeaderView() ? position-1 : position;
        LogUtils.local("onItemClick clickedView=%s, position=%s", clickedView, position);
        SectionDataResponse dataBean = mViewAdapter.getItem(position);
        if (dataBean.mType == SectionDataResponse.kDataTypeLoadMore)
        {
            LogUtils.local("to load more view");
            startActivity(new Intent(this, FragmentDemoActivity.class));
        }
        else if (dataBean.mType == SectionDataResponse.kDataTypeMulti)
        {
            LogUtils.local("to multi view");
        }
        else if (dataBean.mType == SectionDataResponse.kDataTypeScroll)
        {
            LogUtils.local("to scroll view");
        }
    }

    @Override
    public void onItemLongClick(final RecyclerView parent, final View clickedView, int position) {
        position = mViewAdapter.hasHeaderView() ? position-1 : position;
        LogUtils.local("onItemLongClick clickedView=%s, position=%s", clickedView, position);
    }
}
